/*
 * UnitScreen.cpp
 *
 *  Created on: 2013-1-19
 *      Author: heqiu
 */

#include "UnitScreen.h"

namespace guud {
UnitScreen::UnitScreen() {
	m_uiWidth = 0;
	m_uiHeight = 0;
	m_uiWinPixel = 1;
	m_bMoveing = false;
	m_uiLastX = 0;
	m_uiLastY = 0;
}
bool UnitScreen::init(uint32_t uiWidth, uint32_t uiHeight, uint32_t uiBpp,
		uint32_t uiWinPixel, const char * pName) {
	m_uiWidth = uiWidth;
	m_uiHeight = uiHeight;
	m_uiWinPixel = uiWinPixel;
	m_GuudSurface.createScreen(uiWidth, uiHeight, uiBpp);
	m_GuudSurface.setName(pName);
	return true;
}
void UnitScreen::white() {
	m_GuudSurface.whiteScreen();
}
void UnitScreen::show() {
	m_GuudSurface.showScreen();
}
int32_t UnitScreen::getWinPosX() {
	if (m_stPixelPos.m_iX >= 0) {
		return m_stPixelPos.m_iX / m_uiWinPixel;
	} else {
		return -((-m_stPixelPos.m_iX) / m_uiWinPixel) - 1;
	}
}
int32_t UnitScreen::getWinPosY() {
	if (m_stPixelPos.m_iY >= 0) {
		return m_stPixelPos.m_iY * 2 / m_uiWinPixel;
	} else {
		return -((-m_stPixelPos.m_iY) * 2 / m_uiWinPixel) - 1;
	}
}
uint32_t UnitScreen::getWinWidth() {
	return m_uiWidth / m_uiWinPixel;
}
uint32_t UnitScreen::getWinHeight() {
	return m_uiHeight * 2 / m_uiWinPixel;
}

void UnitScreen::drawCoordinate(UnitMap & stUnitMap, GuudFont & stFont) {
	uint32_t uiW = getWinWidth();
	uint32_t uiH = getWinHeight();
	GuudPoint pw(getWinPosX(), getWinPosY());
	GuudPoint pm;
	stUnitMap.transformToMap(pw, pm);
	stUnitMap.transformToScreen(pm, pw);
	uint32_t uiTmp = 1;
	for (int32_t uj = 0; uj < (int32_t) uiH + 4; uj += 2) {
		uiTmp = 1 - uiTmp;
		for (int32_t ui = uiTmp - 2; ui < (int32_t) uiW + 2; ui += 2) {
			drawCoordinate(pw.m_iX + ui, pw.m_iY + uj, stUnitMap, stFont);
		}
	}
}
void UnitScreen::cycle(UnitMap & stUnitMap, void (*func)(int32_t, int32_t)) {
	int32_t iX = getWinPosX();
	int32_t iY = getWinPosY();
	GuudPoint pwin(iX, iY);
	GuudPoint pm;
	stUnitMap.transformToMap(pwin, pm);
	stUnitMap.transformToScreen(pm, pwin);
	uint32_t uiW = getWinWidth();
	uint32_t uiH = getWinHeight();

	uint32_t uiTmp = 1;
	for (int32_t uj = 0; uj < (int32_t) (uiH + 4); uj += 2) {
		uiTmp = 1 - uiTmp;
		for (int32_t ui = 0; ui < (int32_t) (uiW + 4); ui += 2) {
			func(pwin.m_iX + ui + uiTmp, pwin.m_iY + uj);
		}
	}
}
void UnitScreen::drawCoordinate(int32_t iX, int32_t iY, UnitMap & stUnitMap,
		GuudFont & stFont) {
	GuudPoint pw(iX, iY);
	GuudPoint pm;
	stUnitMap.transformToMap(pw, pm);
	if (!stUnitMap.bPointIn(pm)) {
		return;
	}
	{
		SDL_Rect wall;
		wall.x = pw.m_iX * m_uiWinPixel - m_stPixelPos.m_iX;
		wall.y = pw.m_iY * m_uiWinPixel / 2 - m_stPixelPos.m_iY;
		wall.w = m_uiWinPixel * 2 - 1;
		wall.h = m_uiWinPixel - 1;

		SDL_FillRect(m_GuudSurface.m_pSDL_Surface, &wall,
				SDL_MapRGB(m_GuudSurface.m_pSDL_Surface->format, 0x00, 0xff,
						0xff));
		{
			char buf[64];
			sprintf(buf, "(%d-%d)", pw.m_iX, pw.m_iY);
			GuudColor stColor;
			stColor.setColor(0, 0, 0);
			SDL_Surface *message = TTF_RenderText_Solid(stFont.m_font, buf,
					stColor.m_color);
			SDL_BlitSurface(message, NULL, m_GuudSurface.m_pSDL_Surface, &wall);
			SDL_FreeSurface(message);
		}
		{
			wall.y += 10;
			char buf[64];
			sprintf(buf, "(%d-%d)", pm.m_iX, pm.m_iY);
			GuudColor stColor;
			stColor.setColor(255, 0, 255);
			SDL_Surface *message = TTF_RenderText_Solid(stFont.m_font, buf,
					stColor.m_color);
			SDL_BlitSurface(message, NULL, m_GuudSurface.m_pSDL_Surface, &wall);
			SDL_FreeSurface(message);
		}
	}
}
void UnitScreen::drawMarkPoint(GuudPoint pw) {
	SDL_Rect wall;
	wall.x = pw.m_iX * m_uiWinPixel - m_stPixelPos.m_iX;
	wall.y = pw.m_iY * m_uiWinPixel / 2 - m_stPixelPos.m_iY;
	wall.w = m_uiWinPixel * 2 - 1;
	wall.h = m_uiWinPixel - 1;
	SDL_FillRect(m_GuudSurface.m_pSDL_Surface, &wall,
			SDL_MapRGB(m_GuudSurface.m_pSDL_Surface->format, 0x00, 0x00, 0xff));
}
void UnitScreen::drawBuilding(UnitMap & stUnitMap, uint32_t uiNo,
		UnitBuilding * pBuilding) {
	if (pBuilding == NULL) {
		return;
	}
	//不显示
	if (!pBuilding->m_bShow) {
		return;
	}
	//已经绘图
	if (pBuilding->m_uiDrawNo == uiNo) {
		return;
	}
	pBuilding->m_uiDrawNo = uiNo;

	SDL_Rect offset;
	GuudPoint pw;
	stUnitMap.transformToScreen(pBuilding->m_stPos, pw);
	offset.x = ((pw.m_iX)) * m_uiWinPixel - 200 - m_stPixelPos.m_iX;
	offset.y = ((pw.m_iY) / 2) * m_uiWinPixel - 200 - m_stPixelPos.m_iY;
	SDL_BlitSurface(pBuilding->m_pGuudSurface->m_pSDL_Surface, NULL,
			m_GuudSurface.m_pSDL_Surface, &offset);
}

void UnitScreen::drawControl(UnitControl & stUnitControl) {
	if (!stUnitControl.m_bShow) {
		return;
	}
	SDL_Rect offset;
	offset.x = stUnitControl.m_iXPixelPos;
	offset.y = stUnitControl.m_iYPixelPos;
	SDL_BlitSurface(stUnitControl.m_GuudSurface.m_pSDL_Surface, NULL,
			m_GuudSurface.m_pSDL_Surface, &offset);
}

void UnitScreen::mouseMoving(uint16_t uiX, uint16_t uiY) {
	if (m_bMoveing) {
		m_stPixelPos.m_iX -= (uiX - m_uiLastX);
		m_stPixelPos.m_iY -= (uiY - m_uiLastY);
		m_uiLastX = uiX;
		m_uiLastY = uiY;
	}
}
void UnitScreen::mouseUp(uint16_t uiX, uint16_t uiY) {
	m_bMoveing = false;
}
void UnitScreen::mouseDown(uint16_t uiX, uint16_t uiY) {
	m_bMoveing = true;
	m_uiLastX = uiX;
	m_uiLastY = uiY;
}
}
